using UnityEngine;
using System.Collections;

public class ViewGameGUI : MonoBehaviour{
	
	public GameEngine engine;
	
	private int _bestScore;
	
	private tk2dTextMesh _txtBombs;
	private tk2dTextMesh _txtSupports;
	private tk2dTextMesh _txtGold;
	private tk2dTextMesh _txtGoldTotal;
	private tk2dTextMesh _txtBest;
	private tk2dTextMesh _txtMsg1_1;
	private tk2dTextMesh _txtMsg1_2;
	
	private tk2dSprite _logo;
	private tk2dSprite _controls;
	
	protected void Awake(){
		
		_txtBombs = transform.FindChild("TxtBombs").GetComponent<tk2dTextMesh>();
		_txtSupports = transform.FindChild("TxtSupports").GetComponent<tk2dTextMesh>();
		_txtGold = transform.FindChild("TxtGold").GetComponent<tk2dTextMesh>();
		_txtGoldTotal = transform.FindChild("TxtGoldTotal").GetComponent<tk2dTextMesh>();
		_txtBest = transform.FindChild("TxtBest").GetComponent<tk2dTextMesh>();
		_txtMsg1_1 = transform.FindChild("TxtMsg1_1").GetComponent<tk2dTextMesh>();
		_txtMsg1_2 = transform.FindChild("TxtMsg1_2").GetComponent<tk2dTextMesh>();
		
		_logo = transform.FindChild("Logo").GetComponent<tk2dSprite>();
		_controls = transform.FindChild("Controls").GetComponent<tk2dSprite>();
		
		_logo.transform.position += engine.player.transform.position;
		_controls.transform.position += engine.player.transform.position;
		
		_txtMsg1_1.gameObject.SetActiveRecursively(false);
		_txtMsg1_2.gameObject.SetActiveRecursively(false);
		
		engine.EventGotGold += HandleGotGold;
		
		_bestScore = 0;
		if(PlayerPrefs.HasKey("BestScore")){
			_bestScore = PlayerPrefs.GetInt("BestScore",0);
		}
		
	}
	
	protected void FixedUpdate(){
		if(engine.goldCollected>_bestScore){
			_bestScore = engine.goldCollected;
			PlayerPrefs.SetInt("BestScore",_bestScore);
		}
		
		_txtBombs.text = engine.player.bombs.ToString("00.##");
		_txtSupports.text = engine.player.supports.ToString("00.##");
		_txtGold.text = engine.player.gold.ToString("0000.##");
		_txtGoldTotal.text = engine.goldCollected.ToString("000000.##");
		_txtBest.text = _bestScore.ToString("000000.##");
		
		_txtBombs.Commit();
		_txtSupports.Commit();
		_txtGold.Commit();
		_txtGoldTotal.Commit();
		_txtBest.Commit();
	}
	
	private void HandleGotGold(){
		_txtMsg1_1.gameObject.SetActiveRecursively(true);
		_txtMsg1_2.gameObject.SetActiveRecursively(true);
		Invoke("HideMessage1",4f);
	}
	private void HideMessage1(){
		_txtMsg1_1.gameObject.SetActiveRecursively(false);
		_txtMsg1_2.gameObject.SetActiveRecursively(false);	
	}
	
}

